package org.brisling.domain.common;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import org.brisling.domain.sysman.inf.BizBeanDataModeler;
import org.brisling.domain.sysman.inf.UplinkDataModeler;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler"})
public class BaseUplinkDomain<T extends BizBeanDataModeler> extends BaseDomain implements UplinkDataModeler<T> {
	
	@Transient
	@JsonIgnore
	private Class<T> c = null;
	
	public BaseUplinkDomain(){};
	
	public BaseUplinkDomain(Class<T> clazz){
		c = clazz;		
	};
	
	@OneToOne(cascade={CascadeType.REFRESH},fetch = FetchType.LAZY)
    @JoinColumn(name="uplinkid")
	private T uplink ;
	
	@Column(name="uplinkid",insertable=false,updatable=false)
	private Long uplinkid;
	
	public void setUplinkid(Long _uplinkid){
		
		if(_uplinkid!=null){
			if(uplink==null){
				try {
					uplink = c.newInstance();
					
				} catch (InstantiationException | IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			uplink.setId(_uplinkid);
		}
	}
	
	public Long getUplinkid(){
		return uplink==null ? null :uplink.getId();
	}

	
	public T getUplink() {
		// TODO Auto-generated method stub
		return uplink;
	}

	
	public void setUplink(T t) {
		// TODO Auto-generated method stub
		this.uplink = t;
	}	
	
	public String getUplinkName(){
		return uplink!=null?uplink.getName():null;
	}
	
	public void setUplinkName(String _name){
		if(uplink!=null){
			uplink.setName(_name);
		}
	}
	
	public String getUplinkIdentify(){
		return uplink!=null?uplink.getIdentify():null;
	}
	
	public void setUplinkIdentify(String _identify){
		if(uplink!=null){
			uplink.setIdentify(_identify);
		}
	}

}
